package nancal.mp.entity.dev

import nancal.mp.entity.base.TestBase
import nbcp.base.utils.FileUtil
import nbcp.myoql.code.generator.db.mysql.DbrGenerator4Kotlin
import nbcp.myoql.code.generator.db.mysql.SqlDbEntityGenerator
import org.junit.jupiter.api.Test
import java.io.File


class SqlTool : TestBase() {

    @Test
    fun gen_dbr() {
        var list = DbrGenerator4Kotlin.generateEntity(
            "${entityPkgName}.db.mysql.entity.",
            "${ormPkgName}.db.mysql.table",
        )

        var targetEntityPathName = FileUtil.resolvePath(
            File(workPath).parentFile.path + "/${ormProjectName}/src/main/kotlin",
            "${ormPkgName}.db.mysql.table".split(".").joinToString("/")
        )

        File(targetEntityPathName).deleteRecursively();
        File(targetEntityPathName).mkdirs()


        list.forEach {
            writeToFile(
                FileUtil.resolvePath(targetEntityPathName, it.code), it.value
            );
        }

    }

    /**
     * 1. 根据数据库定义生成实体：所有
     */
    @Test
    fun mysql2Entity() {
        SqlDbEntityGenerator.enumPkgs = "com.th.enums.common,com.th.enums.admin,com.th.enums.app,com.th.enums.collection,com.th.enums.pay,com.th.mp.db.enums".split(",")

        var path = File(workPath).parentFile.path + "/${entityProjectName}/src/main/kotlin/${entityPkgPath}/db/mysql/entity"
        SqlDbEntityGenerator.extTableCodeDefines = mapOf("app_lpay" to """
   /**
     * 已还总额度
     *
     * @return
     */
    fun calcPaidAmount(): Double {
        return this.principalPaid + this.interestPaid + this.defaultPaid + this.ivaPaid ;
    }

    /**
     * 应还总额度(不算展期费)
     *
     * @return
     */
    fun calcTotalAmount(): Double {
        return this.principalAccr + this.defaultAccr + this.interestAccr + this.ivaAccr ;
    }

    /**
     * 剩余应还额度
     *
     * @return
     */
    fun calcRemainAmount():Double {
        return calcTotalAmount() - calcPaidAmount();
    }




    /**
     * 已还总展期费
     *
     */
    fun calcRolloverPaidAmount(): Double {
        return this.defaultPaid + this.interestPaid  + this.ivaPaid  + this.rolloverPaid ;
    }

    /**
     * 应还总展期费
     *
     */
    fun calcRolloverAccrAmount(): Double {
        return this.defaultAccr + this.interestAccr  + this.ivaAccr  + this.rolloverAccr ;
    }

    /**
     * 剩余展期费
     *
     * @return
     */
    fun calcRemainRolloverAmount(): Double {
        return calcRolloverAccrAmount() - calcRolloverPaidAmount();
    }

    /**
     * 逾期天数
     *
     * @return
     */
    fun calcOverdueDays(): Int {
        if (this.dueDate == null) {
            return 0;
        }
        return java.time.temporal.ChronoUnit.DAYS.between(this.dueDate!!.toLocalDate(), LocalDate.now()).toInt();// 计算日期差
    }


    /**
     * 在贷天数
     *
     * @return
     */
    fun calcLoanDays(): Int {
        if (this.payDate == null) {
            return 0;
        } 
        
        return java.time.temporal.ChronoUnit.DAYS.between(this.payDate!!.toLocalDate(), LocalDate.now()).toInt();// 计算日期差
    }
    """)

        var list = SqlDbEntityGenerator.db2Entity()
            .whereTable { it != "flyway_schema_history" }

            .toKotlinCode("${entityPkgName}.db.mysql.entity");

        list.forEach {
            writeToFile(FileUtil.resolvePath(path, it.name + ".kt"), it.code);
        }
    }
}
